package src.sort;


import java.util.Arrays;

/**
 * @author starsea
 * @date 2024-07-31 12:22
 */

/**
 * 时间复杂度：O(n*logN)最坏情况下
 * 空间复杂度：O(n)
 * 稳定性：
 */
public class QuickSort {
    public static void main(String[] args) {
    int [] arr={100,89,23,32,45,35};
    quickSort(arr);
        System.out.println(Arrays.toString(arr));

    }
    public static void quickSort(int[] arr)
    {
        quick(arr,0,arr.length-1);

    }
    public static void quick(int [] arr,int left,int right)
    {

        if(left>=right)
        {
            return;
        }
        int point=partitions(arr,left,right);
        quick(arr,left,point-1);
        quick(arr,point+1,right);
    }

    private static int partitions(int[] arr, int left, int right) {
        int tmp=arr[left];
        int i=left;
        while (left<right)
        {
            while(left<right && arr[right]>=tmp)
            {
                right--;
            }
            while(left<right && arr[left]<=tmp)
            {
                left++;
            }
            swaps(arr,left,right);
        }
        swaps(arr,i,left);
        return left;
    }

    private static void swaps(int[] arr, int left, int right) {
        int tmp=arr[left];
        arr[left]=arr[right];
        arr[right]=tmp;
    }
}
